package com.srit.tag;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.springframework.web.context.WebApplicationContext;

import com.srit.core.sql.OrderBy;
import com.srit.forecast.constant.Constant;
import com.srit.system.bean.Code;
import com.srit.system.bean.Grid;
import com.srit.system.bean.GridAttrRel;
import com.srit.system.bean.PageConfig;
import com.srit.system.constant.SystemConstant;
import com.srit.system.dao.CodeDao;
import com.srit.system.dao.CodeTypeDao;
import com.srit.tag.html.Html;

/**
 * 报表生成标签
 * 
 * @author guan
 * 
 */
public class ReportViewTag extends TagSupport {

	private CodeTypeDao codeTypeDao;
	private CodeDao codeDao;

	protected Object getBeanFromSpring(String name) {
		ServletContext context = this.pageContext.getServletContext();
		WebApplicationContext wac = (WebApplicationContext) context
				.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
		return wac.getBean(name);
	}

	public int doStartTag() throws JspException {
		codeTypeDao = (CodeTypeDao) this.getBeanFromSpring("codeTypeDaoImpl");
		codeDao = (CodeDao) this.getBeanFromSpring("codeDaoImpl");
		return TagSupport.EVAL_BODY_INCLUDE;
	}

	@Override
	public int doAfterBody() throws JspException {
		System.out.println("doAfterBody");
		JspWriter out = pageContext.getOut();
		try {
			out.print("hahahahhaha");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doAfterBody();
	}

	@Override
	public int doEndTag() throws JspException {
		JspWriter out = pageContext.getOut();
		PageConfig pageConfig = (PageConfig) pageContext.getRequest()
				.getAttribute(SystemConstant.PAGECONFIG);
		try {
			// Html table = horizontalTable(reportConfig);
			for (Iterator<Grid> it = pageConfig.getGridList().iterator(); it
					.hasNext();) {
				Grid grid = it.next();
				Html result;
				if (Constant.GRID_FORM.equals(grid.getGridType())) {
					result = form(grid);
				} else if (Constant.GRID_LIST_H.equals(grid.getGridType())) {
					result = horizontalTable(grid);
				} else if (Constant.GRID_LIST_V.equals(grid.getGridType())) {
					result = vertical(grid);
				} else {
					result = new Html("div");
					result.setInnerHtml("grid.name为:" + grid.getName()
							+ "  出错 ");
				}
				out.print(result);
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doEndTag();
	}

	private Html form(Grid grid) {
		return null;
	}

	// 横向的table
	private Html horizontalTable(Grid grid) {
		List<Code> areaList;
		if (Constant.GRID_SUBJECT_MANY.equals(grid.getSubjectType())) {
			OrderBy orderBy = new OrderBy();
			areaList = codeDao.listByCodeTypeId(
					Integer.parseInt(grid.getCodeType()), orderBy);
		} else {
			areaList = new ArrayList<Code>();
			// code.setId(Integer.parseInt(grid.getCodeValue()));
			//
			// areaList.add(code);
		}

		Html table = new Html("table");
		table.attr("width", "100%");

		Html tr_Head = new Html("tr");

		if (grid.getEffectTime() > 0) {
			Html time_th = new Html("th");
			time_th.setInnerHtml("时段");
			tr_Head.addSon(time_th);
		}
		if (areaList.size() > 0) {
			Html area_th = new Html("th");
			area_th.setInnerHtml("地区");
			tr_Head.addSon(area_th);
		}

		// 表头部分，根据config的关联来遍历
		for (int i = 0; i < grid.getRelList().size(); i++) {
			GridAttrRel rel = grid.getRelList().get(i);
			Html th = new Html("th");
			th.attr("width", rel.getWidth());
			th.setId(rel.getHtmlId());
			th.setName(rel.getHtmlName());
			th.setInnerHtml(rel.getShowName());
			th.attr("title", rel.getTitle());
			tr_Head.addSon(th);
			// td.attr("width")
		}
		table.addSon(tr_Head);
		// 时段循环
		for (int i = 0; i < grid.getEffectTime(); i++) {
			Html tr = new Html("tr");
			tr.attr("align", "center");
			// 时段
			Html time_td = new Html("td");
			time_td.attr("rowspan", String.valueOf(areaList.size()));
			time_td.setInnerHtml("时段" + i);
			tr.addSon(time_td);
			// 地理+ 属性
			for (int j = 0; j < areaList.size(); j++) {
				Html area_td = new Html("td");
				area_td.setInnerHtml(areaList.get(j).getName());
				if (j == 0) {
					tr.addSon(area_td);
					for (int k = 0; k < grid.getRelList().size(); k++) {
						// Html attr_td = new Html("td");
						// attr_td.setInnerHtml("<input type=text/>");
						tr.addSon(createTdByAttrEl(grid.getRelList().get(k)));
					}
					table.addSon(tr);
				} else {
					Html areaTr = new Html("tr");
					areaTr.attr("align", "center");
					areaTr.addSon(area_td);
					for (int k = 0; k < grid.getRelList().size(); k++) {
						// Html attr_td = new Html("td");
						// attr_td.setInnerHtml("<input type=text/>");
						areaTr.addSon(createTdByAttrEl(grid.getRelList().get(k)));
					}
					table.addSon(areaTr);
				}
			}
		}
		return table;
	}

	private Html vertical(Grid grid) {
		List<String> areaList = new ArrayList<String>();
		areaList.add("江北");
		areaList.add("宁波");
		areaList.add("北仑");
		Html table = new Html("table");
		Html tr_Head = new Html("tr");
		for (int i = 0; i < grid.getRelList().size(); i++) {
			Html th = new Html("th");

		}

		return table;
	}

	private Html createTdByAttrEl(GridAttrRel gridAttrRel) {
		Html td = new Html("td");
		if (gridAttrRel.getAttrId() == null || gridAttrRel.getAttrId() == 0) {
			// 普通的
			td.setInnerHtml("<input type=text name=\""
					+ gridAttrRel.getHtmlName() + "\" id=\""
					+ gridAttrRel.getHtmlId() + "\"/>");
		} else {
			// select的
			td.setInnerHtml("<select>");
		}
		return td;
	}

}
